package com.spudtech
{
    
    import flash.geom.*;
    import flash.display.*;
    
    import com.spudtech.collision.*;
    import com.spudtech.graphics.*;
    
    
    public class SpudEntity extends SpudObject
    {
        
        protected var _collider : SpudCollider;
        
        protected var _graphic : ISpudDrawable;
        
        protected var _scene : SpudScene;
        
        public function SpudEntity(position : Point = null,
                                   init_graphic : ISpudDrawable = null,
                                   init_collider : SpudCollider = null,
                                   graphic_offset : Point = null,
                                   collider_offset : Point = null)
        {
            super();
            
            // Set so the setters don't
            // put up a big fuss
            _graphic = null;
            _collider = null;
            
            // Actually set the graphic
            graphic = SpudObject(init_graphic);
            
            // Actually set the collider
            collider = init_collider;
            
            
            if (position == null)
            {
                position = new Point(0,0);
            }
            if (collider_offset == null)
            {
                collider_offset = new Point(0,0);
            }
            if (graphic_offset == null)
            {
                graphic_offset = new Point(0,0);
            }
            
            this.x = position.x;
            this.y = position.y;
            
            _scene = null;
            
            // Set collider offset
            
            this.graphic.x = graphic_offset.x;
            this.graphic.y = graphic_offset.y;
            
        }
        
        public function get scene() : SpudScene
        {
            return _scene;
        }
        public function set scene(new_scene : SpudScene) : void
        {
            _scene = new_scene;
        }
        
        public function get graphic() : SpudObject
        {
            return SpudObject(_graphic);
        }
        public function set graphic(new_graphic : SpudObject) : void
        {
            if (!(new_graphic is ISpudDrawable))
            {
                throw new TypeError("Graphic must implement ISpudDrawable");
            }
            
            if (new_graphic == this.graphic)
            {
                // That's already our graphic
                return;
            }
            
            if (graphic != null)
            {
                // We already have a graphic
                // Detach it
                
                graphic.destroy();
                
                removeChild(graphic);
                
            }
            
            _graphic = ISpudDrawable(new_graphic);
            
            if (graphic != null)
            {
                // We have a new graphic
                // Attach it
                
                addChild(graphic);
                
                graphic.begin();
                
            }
        }
        
        public function get collider() : SpudCollider
        {
            return _collider;
        }
        public function set collider(new_collider : SpudCollider) : void
        {
            _collider = new_collider;
        }
        
        public function get world() : SpudWorld
        {
            return scene.world;
        }
        
        override public function begin() : void
        {
            // TODO
            super.begin();
        }
        
        override public function destroy() : void
        {
            // TODO
            super.destroy();
        }
        
        
        override public function render(offset : Matrix,
                               clip_rect : Rectangle,
                               draw_graphics : Graphics) : void
        {
            // We might want that matrix
            // for another render operation
            // so we make a clone of it to
            // use in this method
            var to_draw_matrix : Matrix = offset.clone();
            
            // The offset matrix is set by
            // the camera
            // We only want the global
            // position of our graphic
            
            // Get the graphic's position
            // relative to us
            var graphic_pos : Point = new Point(graphic.x,graphic.y);
            
            // Translate that position into
            // global coordinates
            graphic_pos = localToGlobal(graphic_pos);
            
            // Translate our offset by
            // our graphic's position
            // This leaves us with the
            // graphic's position
            // relative to the camera's
            // offset
            offset.translate(graphic_pos.x,graphic_pos.y);
            
            // Call our graphic's render()
            graphic.render(
                offset,
                clip_rect,
                draw_graphics
            );
            
        }
        
        
        override public function update() : void
        {
            // TODO
            super.update();
        }
        
    }
    
}
